package com.pn.warehouse.config;

import com.pn.warehouse.filter.LoginCheckFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.Filter;

//原生Servlet的配置类
@Configuration
public class ServletConfig {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 配置FilterRegistrationBean的bean对象 -- 注册原声servlet中的过滤器
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        //创建FilterRegistrationBean的bean对象
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        //创建自定义过滤器
        LoginCheckFilter loginCheckFilter = new LoginCheckFilter();
        //注入redisTemplate
        //由于LoginCheckFilter中没有注入bean中，因此设置一个set接口，在此类中注入
        loginCheckFilter.setRedisTemplate(redisTemplate);
        //注册到FilterRegistrationBean的bean中
        filterRegistrationBean.setFilter(loginCheckFilter);
        //给过滤器指定拦截的请求
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

}
